package fem3d;

import inf.jlinalg.SolveFailedException;
import inf.text.ArrayFormat;

import java.io.IOException;
import java.util.ArrayList;

import fem2.Constraint;
import fem2.Debugger;
import fem2.Force;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.enu.StressType;
import fem2.material.StVenantKirchhoffMM;
import fem2.observer.StressObserver;
import fem2.pre_and_post.GidMeshGenerator;
import fem2.pre_and_post.GidPostStructural;

/**
 * desmonstrate 8-node hexahedra capability with St-Venant Kirchhoff material
 * 
 * @author hbui
 * 
 */
public class Beam3d {

	static String projDir = "/home/hbui/kratos_janosch";
	static String projName = "beam3d.gid";
	String fn = projDir + '/' + projName + '/' + "mesh2.msh";

	public Model createModel() {
		MeshGenerator mg = new GidMeshGenerator(fn);
		Mesh mesh = mg.getMesh(3);

		mesh.Print();

		Model m = new Model(mesh);

		mesh.setNumberOfFields(3);

		/*
		 * element creation
		 */
		double mu = 1;
		double nu = 0.3;
		double E = 2 * mu * (1 + nu);
		double t = 1.0;
		State ss = State.THREE_DIMENSIONAL;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);
		for (int i = 0; i < mesh.countBlocks(); i++) {
			StructuralElement e = new StructuralElement(mesh.getBlock(i), mm);
			m.addElement(e);
		}

		/*
		 * conditions
		 */
		Constraint c1 = new Constraint(false, false, false);
		ArrayList<Node> side = MeshUtilities.seekNodesOnSurface(mesh, 0.0, 1.0, 0.0, 0.0);
		mesh.setConstraint(c1, side);

		double F = -1.0;
		Node n1 = MeshUtilities.seekNode(mesh, 0.0, 12.0, 0.0);
		Node n2 = MeshUtilities.seekNode(mesh, 1.0, 12.0, 0.0);
		Force f1 = new Force(n1, 0.0, 0, F);
		Force f2 = new Force(n2, 0.0, 0, F);
		m.addLoad(f1);
		m.addLoad(f2);

		return m;
	}

	public static void main(String[] args) throws SolveFailedException, IOException {
		Model m = new Beam3d().createModel();

		Mesh mesh = m.getMesh();

		/*
		 * analysis
		 */
		// Solver solver = new BaitschSolver();
		// mesh.setNumberOfFields(3);
		// M.preAnalysis();
		// M.Initialize();
		// int n = mesh.countDofs();
		// solver.setSize(n);
		// M.assembleKt(solver.getA());
		//
		// double[] r = new double[n];
		// r = M.assembleR(r);
		// double[] u = r.clone();
		// solver.solve(u);
		// M.setX(1, u);
		// M.FinalizeNonlinearIteration();

		/*
		 * analysis
		 */
		Analysis an = new LinearStaticAnalysis(m);
		// ((NewtonRaphsonBasedStrategy) an.getStrategy())
		// .setMaxNewtonRaphsonIteration(1);
		// an.getStrategy().setSolverType(SolverType.MKL_SOLVER);
		an.getStrategy().setEchoLevel(EchoLevelType.OUTPUT);
		an.getStrategy().addObserver(new StressObserver(m));
		an.run();

		/*
		 * post process
		 */
		for (int i = 0; i < mesh.countNodes(); i++) {
			System.out.println(ArrayFormat.format(mesh.getNode(i).getUHatLocal()));
		}

		GidPostStructural post = new GidPostStructural(projDir, projName);
		post.clearAllPostFiles();
		post.writeMesh(m, 0);
		post.writeNodalDisplacementResults(m, 0);
		post.writeGaussPointStressResults(m, 0, StressType.CAUCHY_STRESS);

		Debugger.watch("Analysis completed");
	}
}
